﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class AddExistedCommand
    {
        public static void Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (UIContext.Current.SolutionNode != null && (UIContext.Current.SolutionNode.NodeStatus == NodeStatus.CheckOut || UIContext.Current.SolutionNode.NodeStatus == NodeStatus.Add))
            {
                AddExisted();
            }
            else
            {
                List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
                nodeList.Add(UIContext.Current.SolutionNode);
                CheckOutThread.Start(nodeList, ReportProgress, Completed);
            }
        }
        public static void CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (UIContext.Current.CurrentSelectedNode != null && (UIContext.Current.CurrentSelectedNode.NodeType == NodeType.Solution || UIContext.Current.CurrentSelectedNode.NodeType == NodeType.Folder))
            {
                e.CanExecute = true;
            }
            else
            {
                e.CanExecute = false;
            }
        }
        public static void ReportProgress(Guid id, string name, int current, int total)
        {

        }

        public static void Completed(bool HasError, int successed, int total)
        {
            if (HasError)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(() => {
                    Coolzon.UI.CustomMessageBox.ShowError("方案签出失败，可能存在新版本。");
                });
                return;
            }

            if (UIContext.Current.SolutionNode != null && (UIContext.Current.SolutionNode.NodeStatus == NodeStatus.CheckOut || UIContext.Current.SolutionNode.NodeStatus == NodeStatus.Add))
            {
                AddExisted();
            }
        }

        private static void AddExisted()
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.RestoreDirectory = true;
            System.Windows.Forms.DialogResult result = System.Windows.Forms.DialogResult.Cancel;
            System.Windows.Application.Current.Dispatcher.Invoke(() => { result = fileDialog.ShowDialog(); });

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                if (fileDialog.FileNames != null && fileDialog.FileNames.Length > 0)
                {
                    List<Event.UI.AddedFile> fileList = new List<Event.UI.AddedFile>();
                    foreach (var fullName in fileDialog.FileNames)
                    {
                        string ext = string.Empty;
                        string fileName = System.IO.Path.GetFileNameWithoutExtension(fullName);
                        if (fullName.LastIndexOf('.') > 0)
                        {
                            ext = fullName.Substring(fullName.LastIndexOf('.') + 1);
                        }

                        Event.UI.AddedFile file = new Event.UI.AddedFile(fullName, fileName, ext);
                        fileList.Add(file);
                    }
                    Event.UI.UIEventBus.Publish(new Event.UI.UIAddExistedEvent(fileList));
                }
            }
        }
    }
}
